//
//  MusicView.m
//  OpenEyes
//
//  Created by dlios on 15/9/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MusicView.h"



@implementation MusicView
-(instancetype)initWithFrame:(CGRect)frame{
    self =[super initWithFrame:frame];
    if (self) {
        
        self.scrollview =[[UIScrollView alloc]initWithFrame:self.frame];
        self.scrollview.scrollsToTop = NO;
        self.scrollview.contentSize = CGSizeMake(0, HEIGHT*1.5);
        self.scrollview.bounces = YES;
        self.scrollview.backgroundColor = [UIColor colorWithRed:0.220 green:0.220 blue:0.221 alpha:1.000];
        [self addSubview: self.scrollview];

       
        
        
        self.image =[[UIImageView  alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT/667*300)];
//        self.image.backgroundColor =[UIColor cyanColor];
        self.image.userInteractionEnabled = YES;
        [self.scrollview addSubview:self.image];
        
        self.clearView =[[ UIView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT/667*300)];
        self.clearView.backgroundColor = [UIColor clearColor];
        [self.scrollview addSubview:self.clearView];
        
        
        UIImageView * vi =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 55, 55)];
        vi.center = CGPointMake(self.image.frame.size.width/2, self.image.frame.size.height/2);
        vi.image =[UIImage imageNamed:@"bo"];
        vi.userInteractionEnabled = YES;
        [self.clearView addSubview:vi];
     
        
        //挡住放大图片的剩余
        UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, W *300 , WIDTH , H *400)];
        [backView setBackgroundColor:[UIColor colorWithRed:0.220 green:0.220 blue:0.221 alpha:1.000]];
        backView.userInteractionEnabled = YES;
        [self.scrollview addSubview:backView];
       
        
        //标题
        self.titleLabel = [[UILabel alloc ]initWithFrame:CGRectMake(WIDTH/375*10, self.image.frame.origin.y + HEIGHT/667*300+15, WIDTH, HEIGHT/667 * 50)];
        self.titleLabel.textColor =[UIColor whiteColor];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:19];
        [self.scrollview  addSubview:self.titleLabel];
        
        self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/375*10, self.titleLabel.frame.origin.y+10 , WIDTH/375*355, HEIGHT/667*400)];
        self.contentLabel.textColor =[UIColor whiteColor];
        self.contentLabel.font =[UIFont boldSystemFontOfSize:14];
        [self.scrollview addSubview: self.contentLabel];
        
      
        
        
        self.fadeInView = [[LazyFadeInView alloc] init];
//        self.fadeInView.backgroundColor =[UIColor redColor];
//        self.fadeInView.text = self.contentLabel.text;
        [self.scrollview addSubview:self.fadeInView];
//
        
        [self createButtonWithAddAndShare];
        
        
    }
    return self;
}

-(void)createButtonWithAddAndShare
{
    
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.addButton setImage:[UIImage imageNamed:@"iconfont-Collection"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"iconfont-shoucang1"] forState:UIControlStateSelected];
    [self.scrollview addSubview:self.addButton];
    
    
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareButton addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.shareButton setImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
    [self.shareButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
    [self.scrollview addSubview:self.shareButton];
    
}

-(void)addClick:(UIButton *)sender
{
    
    sender.selected = !sender.selected;
    NSMutableArray * array = [[DataBase shareDataBaseHandle]selectModel];
    if (array.count==0) {
        [[DataBase shareDataBaseHandle]insertModelInfo:self.model];
    }
    else
    {
    for (MusicModel * m in array) {
        if ([m.title isEqualToString:_model.title]) {
            UIAlertView * view = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该收藏已存在" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [view show];
        }
        else
        {
            [[DataBase shareDataBaseHandle]insertModelInfo:self.model];
            UIAlertView * view = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [view show];
        }
    }
    }
    

}

-(void)shareClick:(UIButton *)sender
{
    
    
}

-(void)add:(MusicModel *)mo{
    self.model = mo;
    NSURL *url =[NSURL URLWithString:mo.coverImage];
    [self.image sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"place.png"]];
    self.image.backgroundColor = [UIColor redColor];
    
    self.titleLabel.text = mo.title;
    
    CGFloat heigh = [HeightWithString heightForTextLabel:mo.content width:WIDTH/375*355 fontSize:14];
    
   
    self.fadeInView.frame = CGRectMake(WIDTH/375*10, self.titleLabel.frame.origin.y+ H * 50 , WIDTH/375*355, heigh+40 );
    self.fadeInView.text = mo .content;


    
    self.scrollview.contentSize =CGSizeMake(0, HEIGHT/667*300 + HEIGHT/667 * 50 + heigh + 64);
    
    
//    [self.addButton setFrame:CGRectMake(W * 20, self.titleLabel.frame.origin.y+ H * (80+heigh) , 30, 30)];
    
    [self.addButton setFrame:CGRectMake(W * 20, self.titleLabel.frame.origin.y+ H * 90 + heigh , W * 30, H * 30)];
    
    [self.shareButton setFrame:CGRectMake(W * 80, self.titleLabel.frame.origin.y+ H * 92 + heigh , W * 28, H * 28)];
    [self.addButton addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
